package org.gjy.m8.date;

import com.google.common.base.Stopwatch;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.LongAdder;

/**
 * 比如创建同时适合人与机器的日期和时间，逐渐转入到日期和时间API更高级的一些应用，
 * 比如操纵、解析、打印输出日期-时间对象，使用不同的时区和年历。
 * LocalDate、LocalTime、Instant、Duration和Period。
 * LocalDate，LocalTime，LocalDateTime: 获取非时区日期
 * Instant: 获取时区时间
 * Duration: 获取时间间隔
 * Period: 获取日期间隔
 * 操纵、解析和格式化日期
 *
 * @author 宫静雨
 * @version 1.0
 * @since 2023-08-02 08:48:17
 */
public class DateTest {

    @Test
    public void test14() {
        LocalDateTime now = LocalDateTime.now();
        Date from = Date.from(now.plusDays(30).toInstant(ZoneOffset.of("+8")));
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(from);
        System.out.println(format);
    }

    @Test
    public void test13() {
        Instant instant = LocalDateTime.now().toInstant(ZoneOffset.UTC);
        System.out.println(instant);
    }

    @Test
    public void test12() {
        LocalDate t1 = LocalDate.of(1921, 7, 1);
        LocalDate t2 = LocalDate.of(1949, 12, 1);
        LocalDate t3 = LocalDate.of(1978, 12, 1);
        LocalDate t4 = LocalDate.of(2013, 12, 1);

        int y1 = Period.between(t1, t2).getYears();
        int y2 = Period.between(t2, t3).getYears();
        int y3 = Period.between(t3, t4).getYears();
        System.out.println("y1 = " + y1); // 28
        System.out.println("y2 = " + y2); // 29
        System.out.println("y3 = " + y3); // 35
    }

    @Test
    public void test11() {
        LongAdder la = new LongAdder();

        for (int i = 0; i < 1000; i++) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            byte[] bytes = new byte[1024 * 1024 * 10];
            ThreadLocalRandom.current().nextBytes(bytes);
            long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
            // System.out.println("Yes!" + elapsed + "ms");
            la.add(elapsed);
        }

        System.out.println(la.sum() / 100000);
    }

    @Test
    public void test10() throws Exception {
        for (int i = 0; i < 10; i++) {
            Stopwatch stopwatch = Stopwatch.createStarted();
            TimeUnit.SECONDS.sleep(2);
            stopwatch.stop();
            System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS));
        }
    }

    @Test
    public void test9() {
        LocalDate now = LocalDate.now();
        LocalDate with = now.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(now);
        System.out.println(with);
    }

    @Test
    public void test8() {
        LocalDate now = LocalDate.now();
        LocalDate l1 = now.withYear(2024);
        System.out.println(now);
        System.out.println(l1);
    }

    @Test
    public void test7() {
        Period between = Period.between(LocalDate.of(2023, 8, 2),
                LocalDate.of(2023, 8, 3));
        System.out.println(between.getDays());

        Period period = Period.ofDays(2);
        System.out.println(period);
        System.out.println(period.getDays());
    }

    @Test
    public void test5() {
        Duration duration = Duration.ofDays(1);
        System.out.println(duration.getSeconds());

        Duration between = Duration.between(LocalDateTime.now(),
                LocalDateTime.now().plusDays(2));
        System.out.println(between.getSeconds());
    }

    @Test
    public void test6() {
        long between = ChronoUnit.DAYS.between(LocalDate.of(2023, 8, 2),
                LocalDate.of(2023, 8, 5));
        System.out.println(between);
    }

    @Test
    public void test4() {
        Instant instant = Instant.now();
        ZoneId zoneId = ZoneId.of("Asia/Shanghai");
        ZonedDateTime zonedDateTime = instant.atZone(zoneId);

        System.out.println(zonedDateTime);
        System.out.println(instant);
    }

    @Test
    public void test3() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yy-MM-dd");
//        LocalDate parse = LocalDate.parse("23-08-02", formatter);
//        System.out.println(parse);
        String format = formatter.format(LocalDate.now());
        System.out.println(format);
    }

    @Test
    public void test2() {
        LocalTime now = LocalTime.now();
        System.out.println(now);
        System.out.println(now.getHour());
        System.out.println(now.getMinute());
        System.out.println(now.getSecond());
    }

    @Test
    public void test1() {
        LocalDate now = LocalDate.now();
        System.out.println(now);
        System.out.println(now.get(ChronoField.DAY_OF_WEEK));
        System.out.println(now.isLeapYear());
        System.out.println(now.getYear());
        System.out.println(now.getMonth());
        System.out.println(now.getMonthValue());
        System.out.println(now.getDayOfYear());
        System.out.println(now.getDayOfMonth());
        System.out.println(now.getDayOfWeek());
        System.out.println(now.getDayOfWeek().getValue());
    }
}
